package com.zjuh.goodboy.biz;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.zjuh.goodboy.biz.dataprovider.IncrItemDataProvider;
import com.zjuh.goodboy.claw.JavaClawer;
import com.zjuh.goodboy.config.Configuration;
import com.zjuh.goodboy.config.DefaultConfiguration;
import com.zjuh.goodboy.context.AppContext;
import com.zjuh.goodboy.context.DefaultAppContext;
import com.zjuh.goodboy.filter.filters.ItemParseFilter;
import com.zjuh.goodboy.filter.filters.LinkExportParserFilter;
import com.zjuh.goodboy.index.IncrIndexBuilder;
import com.zjuh.goodboy.index.lucene.LuceneDocumentService;
import com.zjuh.goodboy.misc.FileDateRangeManager;
import com.zjuh.goodboy.scheduler.GoodboyScheduler;
import com.zjuh.goodboy.scheduler.QuartzSchedulerService;
import com.zjuh.goodboy.scheduler.jobs.ClawSchedulerJob;
import com.zjuh.goodboy.scheduler.jobs.IncrDataProvderSchedulerJob;
import com.zjuh.goodboy.store.expr.JdbcItemExprManager;
import com.zjuh.goodboy.store.item.JdbcItemStore;
import com.zjuh.goodboy.store.link.JdbcLinkManager;
import com.zjuh.goodboy.store.link.SimpleNoItemLinkManager;

public class ItemAppContext implements AppContext, ApplicationContextAware {
	
	private DefaultAppContext appContext = new DefaultAppContext();
	
	private ApplicationContext applicationContext;

	public Object getObject(String name) {
		return appContext.getObject(name);
	}

	public List<?> getObjects(String name) {
		return appContext.getObjects(name);
	}
	
	public void init() {
		appContext.putObject("springContext", applicationContext);
		appContext.init(getConfiguration());
	}

	private String indexDir = "d:/tests/indexes/mock";
	
	private String dateRangeFilepath = "d:/tests/daterange/incr_date";

	@SuppressWarnings("unchecked")
	protected Configuration getConfiguration() {
		DefaultConfiguration configuration = new DefaultConfiguration();
		
		configuration.addProperty("date.range.filepath", dateRangeFilepath);
		configuration.addProperty("index.dir", indexDir);
		
		configuration.addNamedClass("clawer", JavaClawer.class);
		configuration.addNamedClass("linkManager", JdbcLinkManager.class);
		configuration.addNamedClass("itemExprManager", JdbcItemExprManager.class);
		configuration.addNamedClass("noItemLinkManager", SimpleNoItemLinkManager.class);
		
		configuration.addNamedClass("itemStore", JdbcItemStore.class);
		configuration.addNamedClassList("filters", Arrays.asList(LinkExportParserFilter.class, ItemParseFilter.class));
		
		configuration.addNamedClass("schedulerService", QuartzSchedulerService.class);
		configuration.addNamedClassList("schedulerJobs", Arrays.asList(ClawSchedulerJob.class, IncrDataProvderSchedulerJob.class));
		configuration.addNamedClass("goodboyScheduler", GoodboyScheduler.class);
		
		configuration.addNamedClass("documentService", LuceneDocumentService.class);
		configuration.addNamedClass("incrIndexBuilder", IncrIndexBuilder.class);
		configuration.addNamedClass("dateRangeManager", FileDateRangeManager.class);
		configuration.addNamedClass("incrDataProvider", IncrItemDataProvider.class);
		
		return configuration;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	@Override
	public void init(Configuration configuration) {
		
	}

}
